package com.prs.crm.action;

import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;

import org.courser.struts.XJAction;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.FlowEntity;
import com.prs.crm.domain.flow.WorkFlowInstance;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.flow.WorkFlowProcessNode;
import com.prs.crm.domain.flow.WorkFlowProcessNodeDetail;
import com.prs.crm.service.flow.WorkFlowInstanceService;
import com.prs.crm.service.flow.WorkFlowProcessService;
import com.prs.crm.service.hr.DeptService;
import com.prs.crm.service.hr.RoleService;
import com.prs.crm.service.sys.FormService;

/**
 * 流程Action基类
 * 
 * @author courser.tijichen
 * 
 */
public abstract class FlowAction<T extends FlowEntity> extends XJAction {

	private static final long serialVersionUID = -4022635729767849578L;

	private DeptService deptService;

	private RoleService roleService;

	private WorkFlowInstanceService workflowInstanceService;

	private WorkFlowProcessService workflowProcessService;

	private WorkFlowInstance flowInstance;

	private FormService formService;

	private WorkFlowProcess flowProcess;

	private Collection<WorkFlowProcessNode> endNodes;

	private File attach;

	private String attachFileName;

	private WorkFlowProcessNode node;

	private WorkFlowProcessNodeDetail detail;

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public WorkFlowInstanceService getWorkflowInstanceService() {
		return workflowInstanceService;
	}

	public void setWorkflowInstanceService(
			WorkFlowInstanceService workflowInstanceService) {
		this.workflowInstanceService = workflowInstanceService;
	}

	public void setFlowInstance(WorkFlowInstance flowInstance) {
		this.flowInstance = flowInstance;
	}

	public WorkFlowInstance getFlowInstance() {
		return flowInstance;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	public FormService getFormService() {
		return formService;
	}

	public void setDetail(WorkFlowProcessNodeDetail detail) {
		this.detail = detail;
	}

	public void setAttach(File attach) {
		this.attach = attach;
	}

	public File getAttach() {
		return attach;
	}

	public void setAttachFileName(String attachFileName) {
		this.attachFileName = attachFileName;
	}

	public String getAttachFileName() {
		return attachFileName;
	}

	public void setWorkflowProcessService(
			WorkFlowProcessService workflowProcessService) {
		this.workflowProcessService = workflowProcessService;
	}

	public WorkFlowProcessService getWorkflowProcessService() {
		return workflowProcessService;
	}

	public void setFlowProcess(WorkFlowProcess flowProcess) {
		this.flowProcess = flowProcess;
	}

	public WorkFlowProcess getFlowProcess() {
		return flowProcess;
	}

	public void setNode(WorkFlowProcessNode node) {
		this.node = node;
	}

	public WorkFlowProcessNode getNode() {
		return node;
	}

	public void setEndNodes(Collection<WorkFlowProcessNode> endNodes) {
		this.endNodes = endNodes;
	}

	public Collection<WorkFlowProcessNode> getEndNodes() {
		return endNodes;
	}

	@SuppressWarnings({ "unchecked", "unused" })
	private Class getEntityClass() {
		return (Class) ((ParameterizedType) getClass().getGenericSuperclass())
				.getActualTypeArguments()[0];
	}

	public WorkFlowProcessNodeDetail getDetail() {

		if (attach != null) {
			Attach at = FileUtil.getInstance().saveFile(attach, attachFileName,
					"", Constrains.UPLOAD_DIR);
			Attachment attachment = new Attachment();
			attachment.setPath(at.getSrc());
			attachment.setName(attachFileName);
			if (detail == null)
				detail = new WorkFlowProcessNodeDetail();
			detail.setAttachment(attachment);
		}
		return detail;
	}

	protected void displayEndNodes() {
		//System.out.println("flowaction displayEndNodes"+"  1");
		this.setEndNodes(this.getWorkflowProcessService().getNodeByStatus(
				node.getFlowProcess(), Constrains.WORKFLOWPROCESS_STATE_END));
	}

	protected void displayEndNodes(FlowEntity entity) {
		this.setEndNodes(this.getWorkflowProcessService().getNodeByStatus(
				entity.getProcess(), Constrains.WORKFLOWPROCESS_STATE_END));
	}

}
